﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using Dez;

public partial class Default2 : System.Web.UI.Page
{
    int OrderCode;
    int fHouse;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ASPxWebControl.RegisterBaseScript(this);
            grd2.DataSourceID = "ds_WORKSPECLIST";
            grd2.DataBind();

            if (Request.QueryString["CODE"] != null)
            {
                OrderCode = Convert.ToInt32(Request.QueryString["CODE"]);
                Session["OrderCode"] = OrderCode;
            }
            else { OrderCode = -1; }

            GetInfoByOrder(OrderCode);
        }
        else { OrderCode = (int)Session["OrderCode"]; }
    }

    protected void edit_work_Click(object sender, EventArgs e)
    {
        grd2.StartEdit(grd2.FocusedRowIndex);
    }
    protected void del_work_Click(object sender, EventArgs e)
    {
        grd2.DeleteRow(grd2.FocusedRowIndex); 
    }

    protected void grd2_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "KONTRAGENT")
        {
            var combo = (ASPxComboBox)e.Editor;
            var col = e.Column;

            //если подрядчик
            if (Profile.UserInfo.KontragentType == 1)
            {
                combo.Value = Profile.UserInfo.ActiveKontragent;
                combo.Enabled = true;
                //combo.ReadOnly = true;
                //скрываем поле для редактирования KONTRAGENT из формы редактирования
                col.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.False;
            } //иначе (если дез)
            else
            {
                //combo.Value = cbb_kontr.Value;
               // col.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
            }
        }

    }
    protected void add_work_Click1(object sender, EventArgs e)
    {
       grd2.AddNewRow();
    }
    protected void ds_WORKSPECLIST_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        //if (e.Command.Parameters["KONTRAGENT"].Value == null)
        e.Command.Parameters["KONTRAGENT"].Value = Profile.UserInfo.ActiveKontragent;
            e.Command.Parameters["HOUSE"].Value = fHouse;
            if (OrderCode <= 0)
                OrderCode = Convert.ToInt32(Request.QueryString["CODE"]);

            e.Command.Parameters["WORKLIST"].Value = OrderCode;
            e.Command.Parameters["FDATA"].Value = DateTime.Now ;
    }
    protected void grd2_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
        /*e.NewValues["KONTRAGENT"] = Profile.Users.ActiveKontragent;
        e.NewValues["HOUSE"] = 215;
        e.NewValues["WORKLIST"] = OrderCode;*/
    }
    #region получаем информацию о наряде
    protected void GetInfoByOrder(int OrderCode) 
    {
        DEZ context = new DEZ();
        var query = from p in context.WORKLIST
                    where p.CODE == OrderCode
                    join h in context.HOUSE on p.HOUSE equals h.CODE
                    join s in context.STREET on h.STREET equals s.CODE
                    join k in context.KONTRAGENT on p.KONTRAGENT equals k.CODE
                    select new { p.HOUSE, s.NAME, h.NOMER, p.CODE, p.DATA, KNAME = k.NAME};
        foreach (var item in query)
        {
            fHouse = item.HOUSE;
            lbl_OrderInfo.Text = String.Format("Спецификация наряда от {0} по адресу: {1}, {2} ({3})", item.DATA.ToShortDateString(), item.NAME, item.NOMER, item.KNAME);
        }
    }
    #endregion
    protected void grdMAT_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        object WorkIDValue = grd2.GetRowValues(Convert.ToInt32(e.Parameters), "CODE");
        Session["WorkID"] = WorkIDValue;
        //grdMAT.DataSource = ds_WORKSPECMATERIAL;
        grdMAT.PageIndex = 0;
        grdMAT.DataBind();
    }
    protected void ds_CLASSMAT_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        //ds_CLASSMAT.InsertParameters["WORKSPECLIST"].DefaultValue = Session["WorkID"].ToString();
    }
    protected void ds_WORKSPECMATERIAL_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        e.Command.Parameters["WORKSPECLIST"].Value = Session["WorkID"];
        if (e.Command.Parameters["QUANTITY"].Value == DBNull.Value) 
        {
            e.Command.Parameters["QUANTITY"].Value = 0;        
        }
        if (e.Command.Parameters["PRICE"].Value == DBNull.Value)
        {
            e.Command.Parameters["PRICE"].Value = 0;
        }
    }
}